package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpSellerCollectProductSkuBo;
import com.erp.gather.domain.vo.ErpSellerCollectProductSkuVo;
import com.erp.gather.mapper.ErpSellerCollectProductSkuMapper;
import com.erp.gather.service.IErpSellerCollectProductSkuService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpSellerCollectProductSku;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 个人采集商品SKUService业务层处理
 *
 * @author erp
 * @date 2023-06-20
 */
@RequiredArgsConstructor
@Service
public class ErpSellerCollectProductSkuServiceImpl implements IErpSellerCollectProductSkuService {

    private final ErpSellerCollectProductSkuMapper baseMapper;

    /**
     * 查询个人采集商品SKU
     */
    @Override
    public ErpSellerCollectProductSkuVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询个人采集商品SKU列表
     */
    @Override
    public TableDataInfo<ErpSellerCollectProductSkuVo> queryPageList(ErpSellerCollectProductSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerCollectProductSku> lqw = buildQueryWrapper(bo);
        Page<ErpSellerCollectProductSkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询个人采集商品SKU列表
     */
    @Override
    public List<ErpSellerCollectProductSkuVo> queryList(ErpSellerCollectProductSkuBo bo) {
        LambdaQueryWrapper<ErpSellerCollectProductSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerCollectProductSku> buildQueryWrapper(ErpSellerCollectProductSkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerCollectProductSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpSellerCollectProductSku::getSkuCode, bo.getSkuCode());
        lqw.like(StringUtils.isNotBlank(bo.getSkuName()), ErpSellerCollectProductSku::getSkuName, bo.getSkuName());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuNameEn()), ErpSellerCollectProductSku::getSkuNameEn, bo.getSkuNameEn());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuImage()), ErpSellerCollectProductSku::getSkuImage, bo.getSkuImage());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrlList()), ErpSellerCollectProductSku::getImageUrlList, bo.getImageUrlList());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectSkuPrice()), ErpSellerCollectProductSku::getCollectSkuPrice, bo.getCollectSkuPrice());
        lqw.eq(bo.getSkuLength() != null, ErpSellerCollectProductSku::getSkuLength, bo.getSkuLength());
        lqw.eq(bo.getSkuWidth() != null, ErpSellerCollectProductSku::getSkuWidth, bo.getSkuWidth());
        lqw.eq(bo.getSkuHeight() != null, ErpSellerCollectProductSku::getSkuHeight, bo.getSkuHeight());
        lqw.eq(bo.getSkuWeight() != null, ErpSellerCollectProductSku::getSkuWeight, bo.getSkuWeight());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuLink()), ErpSellerCollectProductSku::getSkuLink, bo.getSkuLink());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuBuyLink()), ErpSellerCollectProductSku::getSkuBuyLink, bo.getSkuBuyLink());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuPrice()), ErpSellerCollectProductSku::getSkuPrice, bo.getSkuPrice());
        lqw.eq(bo.getSkuStock() != null, ErpSellerCollectProductSku::getSkuStock, bo.getSkuStock());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuPropertyIds()), ErpSellerCollectProductSku::getSkuPropertyIds, bo.getSkuPropertyIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerCollectProductSku::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增个人采集商品SKU
     */
    @Override
    public Boolean insertByBo(ErpSellerCollectProductSkuBo bo) {
        ErpSellerCollectProductSku add = BeanUtil.toBean(bo, ErpSellerCollectProductSku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人采集商品SKU
     */
    @Override
    public Boolean updateByBo(ErpSellerCollectProductSkuBo bo) {
        ErpSellerCollectProductSku update = BeanUtil.toBean(bo, ErpSellerCollectProductSku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerCollectProductSku entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除个人采集商品SKU
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
